package homework.exp04;

import java.util.Scanner;
import java.util.Stack;
 
public class D {
    public static void main(String[] args) {
        Scanner cin = new Scanner(System.in);
        while (cin.hasNext()) {
            String s = cin.next();
            if ("#".equals(s)) return;
            System.out.println(toPostTree(s));
        }
        cin.close();
    }
 
    private static class Node {
        Object value;
        Node l;
        Node r;
 
        Node(Object value) {
            this.value = value;
        }
    }
 
    //表达式树
    private static String toPostTree(String str) {
        Stack<Node> stack = new Stack<>();
        String S = toPostFix(str);
        String[] arr = S.split(" ");
        for (String s : arr) {
            if ("+".equals(s) || "-".equals(s) || "*".equals(s) || "/".equals(s) || "^".equals(s)) {
                //取出两个数字
                Node node2 = stack.pop();
                Node node1 = stack.pop();
                Node root = new Node(s);
                root.l = node1;
                root.r = node2;
                stack.push(root);
            } else {
                Node node = new Node(s);
                stack.push(node);
            }
        }
        Node root = stack.pop();
        StringBuilder sb = new StringBuilder();
        print(root, sb);
        return sb.toString();
    }
 
    //打印表达式树
    private static void print(Node root, StringBuilder sb) {
        if (root == null) return;
        if (root.l != null &&
                (level((root.l.value + "").toCharArray()[0]) < level((root.value + "").toCharArray()[0]))) {
            sb.append("(");
            print(root.l, sb);
            sb.append(")");
        } else print(root.l, sb);
        //如果当前
        sb.append(root.value);
        if (root.r != null && (root.value.equals("-")
                && level((root.value + "").toCharArray()[0]) >= level((root.r.value + "").toCharArray()[0]) ||
                root.value.equals("/") && level((root.value + "").toCharArray()[0]) >= level((root.r.value + "").toCharArray()[0])
                || level((root.r.value + "").toCharArray()[0]) < level((root.value + "").toCharArray()[0]))) {
            sb.append("(");
            print(root.r, sb);
            sb.append(")");
        } else print(root.r, sb);
    }
 
    //将中缀表达式转换为后缀表达式
    private static String toPostFix(String str) {
        StringBuilder sb = new StringBuilder();
        Stack<Character> stack = new Stack<>();
        for (int i = 0; i < str.length(); ) {
            char c = str.charAt(i);
            switch (c) {
                case '+':
                case '-':
                case '*':
                case '/':
                case '^':
                    while (!stack.isEmpty() && level(c) <= level(stack.peek()) && stack.peek() != '(') {
                        sb.append(stack.pop()).append(" ");
                    }
                    stack.push(c);
                    i++;
                    break;
                case '(':
                    stack.push(c);
                    i++;
                    break;
                case ')':
                    while (stack.peek() != '(') {
                        sb.append(stack.pop()).append(" ");
                    }
                    stack.pop();
                    i++;
                    break;
                default:
                    //取出数字
                    int num = 0;
                    while (c >= '0' && c <= '9') {
                        num = num * 10 + (c - '0');
                        if (i + 1 < str.length()) {
                            i++;
                            c = str.charAt(i);
                        } else {
                            i++;
                            break;
                        }
                    }
                    sb.append(num).append(" ");
            }
        }
        while (!stack.isEmpty()) {
            sb.append(stack.pop()).append(" ");
        }
        return sb.toString().trim();
    }
 
    //操作符等级
    private static int level(Character c) {
        switch (c) {
            case '+':
            case '-':
                return 1;
            case '*':
            case '/':
                return 2;
            case '^':
                return 3;
        }
        return Integer.MAX_VALUE;
    }
}